package com.orangelife.utils;

import java.util.Date;

public class UUIDGenerator {

	public static void main(String[] args) {
		System.out.println(nextOrderNum());
		System.out.println("14842786213261001".length());
		System.out.println(UUIDGenerator.nextCode());
		System.out.println(UUIDGenerator.nextCode());
		System.out.println(UUIDGenerator.nextCode());
	}

	public static synchronized String nextCode() {
		String str = (new Date().getTime()) + "";
		long m = (long) (Math.random() * 9000 + 1000);
		return str + m;
	}

	private static Object lockObj = "lockerOrder";
	/**
	 * 订单号生成计数器
	 */
	private static long orderNumCount = 0L;
	private int maxPerMSECSize = 1000;

	public String makeOrderNum() {
		String finOrderNum = "";
		try {
			// 最终生成的订单号

			synchronized (lockObj) {
				// 取系统当前时间作为订单号变量前半部分，精确到毫秒
				long nowLong = new Date().getTime();
				// 计数器到最大值归零，可扩展更大，目前1毫秒处理峰值1000个，1秒100万
				if (orderNumCount > maxPerMSECSize) {
					orderNumCount = 0L;
				}
				// 组装订单号
				String countStr = maxPerMSECSize + orderNumCount + "";
				finOrderNum = nowLong + countStr;
				orderNumCount++;
				return finOrderNum;

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return finOrderNum;
	}

	public static synchronized String nextOrderNum() {
		UUIDGenerator uuidGenerator = new UUIDGenerator();
		return uuidGenerator.makeOrderNum();
	}

}
